//
//  searchView.m
//  sdk2.0zhengquandasha
//
//  Created by 徐亚东 on 15/12/22.
//  Copyright © 2015年 palmaplus. All rights reserved.
//

#import "searchView.h"
@interface searchView ()<UIAlertViewDelegate>

@end

@implementation searchView

-(instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        [self initAll];
    }
    return self;
}

-(void)initAll{
    CGSize imageSize = CGSizeMake(25, 25);
    CGSize labelSie = CGSizeMake(150, 30);
    CGFloat topGap = 20;
    self.hotImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, topGap, imageSize.width, imageSize.height)];
    self.hotImageView.image = [UIImage imageNamed:@"ico_fire"];
    [self addSubview:self.hotImageView];
    
    self.hotTitleLab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.hotImageView.frame)+10, topGap, 100, imageSize.height)];
    self.hotTitleLab.font = [UIFont systemFontOfSize:20];
    self.hotTitleLab.textColor = UIColorRGB(102, 102, 102, 1);
    self.hotTitleLab.text = @"热门搜索";
    [self addSubview:self.hotTitleLab];
    
    self.historyImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 50, imageSize.width, imageSize.height)];
    self.historyImageView.image = [UIImage imageNamed:@"ico_history"];
    [self addSubview:self.historyImageView];
    
    self.historyTitleLab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.hotImageView.frame)+10, 50, 100, imageSize.height)];
    self.historyTitleLab.font = [UIFont systemFontOfSize:20];
     self.historyTitleLab.textColor = UIColorRGB(102, 102, 102, 1);
    self.historyTitleLab.text = @"搜索历史";
    [self addSubview:self.historyTitleLab];
    
    self.deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.deleteBtn setBackgroundImage:[UIImage imageNamed:@"ico_delete"] forState:UIControlStateNormal];
    self.deleteBtn.frame = CGRectMake(KWIDTH - 10 - imageSize.width, CGRectGetMaxY(self.historyTitleLab.frame), imageSize.width, imageSize.height);
    [self.deleteBtn addTarget:self action:@selector(deleteAction) forControlEvents:UIControlEventTouchUpInside];
    self.deleteBtn.hidden = YES;
    [self addSubview:self.deleteBtn];
    
    self.emptyHistoryLab = [[UILabel alloc]initWithFrame:CGRectMake((KWIDTH - labelSie.width)/2, CGRectGetMaxY(self.historyTitleLab.frame)+10, labelSie.width, labelSie.height)];
    self.emptyHistoryLab.text = @"暂无搜索历史";
     self.emptyHistoryLab.textColor = UIColorRGB(102, 102, 102, 1);
    self.emptyHistoryLab.textAlignment = NSTextAlignmentCenter;
    self.emptyHistoryLab.font = [UIFont systemFontOfSize:20];
    self.emptyHistoryLab.hidden = NO;
    [self addSubview:self.emptyHistoryLab];
}

-(void)deleteAction{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确定删除历史记录？    " delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}

-(void)setHotArr:(NSMutableArray *)hotArr{
    if (!hotArr) {
        return;
    }
    _hotArr = hotArr;
    CGFloat gapF = 7;
    CGSize btnSize = CGSizeMake((KWIDTH - 5 * gapF)/4, 40);
    //CGSize btnSize = CGSizeMake(74, 36);
    CGFloat tempF = 100;
    for (int i = 0;i<hotArr.count;i++) {
        int row = i /4;
        int column = i%4;
        [self createBtn:CGRectMake( gapF + column * (btnSize.width +gapF),CGRectGetMaxY(self.hotTitleLab.frame) + row * (btnSize.height + gapF) + gapF +gapF, btnSize.width, btnSize.height) title:hotArr[i] withTag:-1];
        tempF = CGRectGetMaxY(self.hotTitleLab.frame) + (row + 1) * (btnSize.height + gapF) + gapF + 20 ;
    }
    CGRect rect = self.historyImageView.frame;
    self.historyImageView.frame = CGRectMake(rect.origin.x, tempF, rect.size.width, rect.size.height);
    rect = self.historyTitleLab.frame;
    self.historyTitleLab.frame = CGRectMake(rect.origin.x, tempF, rect.size.width, rect.size.height);
    rect = self.deleteBtn.frame;
    self.deleteBtn.frame = CGRectMake(rect.origin.x, tempF, rect.size.width, rect.size.height);
    rect = self.emptyHistoryLab.frame;
    self.emptyHistoryLab.frame = CGRectMake(rect.origin.x, tempF + 80, rect.size.width, rect.size.height);

}

-(void)setHistoryArr:(NSMutableArray *)historyArr{
        [self deleteAllHistory];
    if (!historyArr || historyArr.count == 0) {
        _historyArr = historyArr;
        return;
    }
    self.deleteBtn.hidden = NO;
    _historyArr = historyArr;
    CGFloat gapF = 10;
    CGSize btnSize = CGSizeMake((KWIDTH - 5 * gapF)/4, 40);
    CGFloat tempF = 100;
    for (int i = 0;i<historyArr.count;i++) {
        int row = i /4;
        int column = i%4;
        [self createBtn:CGRectMake( gapF + column * (btnSize.width +gapF),CGRectGetMaxY(self.historyImageView.frame) + row * (btnSize.height + gapF) + gapF, btnSize.width, btnSize.height) title:historyArr[i] withTag:100 + i];
        tempF = CGRectGetMaxY(self.historyImageView.frame) + (row + 1) * (btnSize.height + gapF) + gapF;
    }
    self.emptyHistoryLab.hidden = YES;
    self.deleteBtn.hidden = NO;
}

-(void)deleteAllHistory{
    if (_historyArr.count > 0) {
        for (int i =0; i<_historyArr.count; i++) {
            UIButton *btn = [self viewWithTag:100 + i];
            [btn removeFromSuperview];
        }
    }
}

-(void)createBtn:(CGRect)frame title:(NSString *)title withTag:(int)tag{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    btn.layer.cornerRadius = 5;
    btn.layer.borderWidth = 1;
    btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    btn.tag = tag;
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    [btn setTitleColor:UIColorRGB(102, 102, 102, 1) forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(subBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
}

-(void)subBtnAction:(UIButton *)btn{
   [SVProgressHUD showWithStatus:@"玩命搜索中..."];
    NSLog(@"%@",btn.titleLabel.text);
    NSString *str = btn.titleLabel.text;
    NSLog(@"---%@",str);
    if (self.searchBlock) {
        self.searchBlock(str);
    }

}

#pragma mark- alertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        NSLog(@"清空历史记录");
        if (self.deleteBlock) {
            self.deleteBlock();
        }
    }
}

@end
